package com.example.a79273.shandianban.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.model.XianHuoBean;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * Created by 79273 on 2016/9/2.
 */
public class XianHuoListAdapter extends BaseAdapter {
    private HashMap<String, List<XianHuoBean.DataBean.ListsBean>> mData;
    private LayoutInflater inflater;
    private Context mContext;

    public XianHuoListAdapter(Context context, HashMap<String, List<XianHuoBean.DataBean.ListsBean>> data) {
        mData = data;
        inflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public List<XianHuoBean.DataBean.ListsBean> getItem(int position) {
        Set<String> titles = mData.keySet();
        String[] titleArr = new String[titles.size()];
        return mData.get((titles.toArray(titleArr))[position]);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_product, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        List<XianHuoBean.DataBean.ListsBean> beanList = getItem(position);

        holder.title_tv.setText(beanList.get(0).getFathername());
        holder.gridAdapter = new XianHuoGridAdapter(beanList, mContext);
        holder.content_gv.setAdapter(holder.gridAdapter);
        return convertView;
    }

    static class ViewHolder {
        private TextView title_tv;
        private GridView content_gv;

        private XianHuoGridAdapter gridAdapter;

        public ViewHolder(View view) {
            title_tv = (TextView) view.findViewById(R.id.tv_productItem_title);
            content_gv = (GridView) view.findViewById(R.id.gv_productItem_content);
        }
    }
}
